<?php
###############   COPYLEFT GPLv3 LICENSE   ###############
##
## JFX Version 0.2.9
## Website Management Software
## www.jfxcms.com
##
## Copyright 2009 GPLv3 - http://www.opensource.org/licenses/gpl-3.0.html
##
## Anthony Gallon
## oi_antz@hotmail.com
##
## Permission is hereby granted to any person having a copy of this software
## to freely use and modify as required so long as the copyright notices
## and branding remain intact.
##
## Full license details available at http://www.jfxcms.com/license
##
###############   COPYLEFT GPLv3 LICENSE   ################

$VIEW->template = $dirname.'/templates/thickbox.tpl';

ob_start();


if(Antz_IntelliForm::submitted('create-menu')){
    $error = false;
  
    $keyname = preg_replace('/[^a-zA-Z0-9]/', '', post('keyname'));
    $title = post('title');
    $template = $_POST['template'];
    $parentid = (int) post('parent');

    if($DB->countRows($CONFIG->dbprefix.'jfxmenu_menus', "keyname = '{$keyname}'")>0){
        $error = true;
        JFX::addError($this->lang('keyname_already_registered'));
        JFX::addFormError('keyname');
    }

    if($DB->countRows($CONFIG->dbprefix.'pages', "id = '{$parentid}'")==0){
        $error = true;
        JFX::addError($this->lang('invalid_parent'));
    }

    if(!$error){
        $dbParams = array(
            'keyname' => $keyname,
            'template' => $template,
            'parent_id' => $parentid
        );
        $this->updateContentLang('title', $keyname, $title, '*', '*');
        $DB->insert($CONFIG->dbprefix.'jfxmenu_menus', $dbParams);
        JFX::addSuccess($this->lang('new_menu_success'));
    }

}


$VIEW->addJs('
$(document).ready(function(){
    $("#jfxcore-new-menu-tree").treeview({
        persist: "location",
        collapsed: true,
        unique: true
    });
    $("#jfxcore-new-menu-tree a").click(function(){
        $("#parent-id").get(0).value = this.id.replace("page_", "");
        $("#disabled-parent").get(0).value = $(this).text();
    });
});
');





$pagesRecursive = JFX::getPageTree(0, $USER->workspace);

$tree = '<ul id="jfxcore-new-menu-tree">'.JFXMenu_MakeTree($pagesRecursive, 'page_').'</ul>';
$parentTitle = $LANG->getContent('core', 'heading', post('parent', $mData['parent_id']));

    $parent = array(
        'name' => 'parent',
        'value' => post('parent', $pData['parent_id']),
        'type' => 'hidden',
        'id' => 'parent-id',
        'label' => ''
    );
    $parentList = array(
        'type' => 'longtag',
        'tag' => 'div',
        'class' => 'form-field',
        'id' => 'parent-page-tree',
        'content' => '<label>'.$LANG->get('core', 'page_parent').'</label>
<div class="input-area">'.$tree.'</div>'
    );
    $selectedParent = array(
        'name' => 'disabled_parent',
        'value' => $parentTitle,
        'type' => 'text',
        'id' => 'disabled-parent',
        'extra' => array(
            array(
                'command' => 'addAttribute',
                'params' => array(
                    '"disabled"', '"disabled"'
                )
            )
        ),
        'label' => $this->lang('selected_parent')
    );






$formFields = array(
    array(
        'type' => 'text',
        'name' => 'keyname',
        'value' => post('keyname', 'mymenu'),
        'label' => $this->lang('new_keyname')
    ),
    array(
        'type' => 'text',
        'name' => 'title',
        'value' => post('title', 'My Menu'),
        'label' => $this->lang('new_title')
    ),
    $parent,
    $parentList,
    $selectedParent,
    array(
        'type' => 'code',
        'id' => 'jfxmenunew',
        'class' => 'html',
        'name' => 'template',
        'value' => post('template', $this->lang('default_template'), 5000),
        'label' => $this->lang('new_template')
    ),
    array(
        'type' => 'submit',
        'name' => 'submitBtn',
        'value' => $this->lang('new_submit'),
        'label' => '&nbsp;'
    )
);

echo JFX::makeRapidForm($formFields, 'New Menu Details', 'create-menu');


$content = ob_get_contents();
ob_end_clean();

$SMARTY->assign('_CST_BODY', $content.' ------');























function JFXMenu_MakeTree($arr, $idprefix){
    $CONFIG = JFX::registry('config');
    $LANG = JFX::registry('lang');
    $returnString = '';
    foreach($arr as $k=>$v){

        $link = '<a href="javascript: void(null)" id="'.$idprefix.$v['id'].'">'.$LANG->getContent('core', 'heading', $v['id']).'</a>';
        if(isset($v['subpages']) && count($v['subpages'])>0){
            $subpages = '<ul>'.JFXMenu_MakeTree($v['subpages'], $idprefix).'</ul>';
        }else{
            $subpages = '';
        }
        $returnString .= '<li>'.$link.$subpages.'</li>';
    }

    return $returnString;
}


